package com.quickask.dao;

import com.quickask.common.Pager;
import com.quickask.entity.AsknowQuestionLocationSetEntity;
import com.quickask.entity.AsknowQuestionSetEntity;
import com.quickask.idao.IQuestionSetDao;
import com.quickask.operate.data.Constants;
import com.quickask.operate.data.ResultCode;
import com.quickask.operate.encode.Geohash;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/9/30.
 */
public class QuestionSetDao extends BaseDao<AsknowQuestionSetEntity, Integer> implements IQuestionSetDao {

    /**
     * 添加问题实体
     * @param asknowQuestionSetEntity 问题实体
     * @return
     */
    @Override
    public void addQuestionEntity(AsknowQuestionSetEntity asknowQuestionSetEntity) {
        save(asknowQuestionSetEntity);
    }

    /**
     * 为问题添加最佳答案
     *
     * @param questionId 问题id
     * @param answerId   最佳答案id
     * @return
     */
    @Override
    public int addFavouriteAnswer(int questionId, int answerId) {
        return queryHql("update AsknowQuestionSetEntity aqse set aqse.answerId=? aqse.questionIsSolved=1 where aqse.questionId = ?", answerId, questionId);
    }

    /**
     * 更新问题同问数
     *
     * @param questionId
     * @return
     */
    @Override
    public int updateQuestionAsknum(int questionId) {
        AsknowQuestionSetEntity asknowQuestionSetEntity = get(questionId);
        if(asknowQuestionSetEntity == null) {
            return ResultCode.QUESTIONID_ERROR;
        }
        int asknum = asknowQuestionSetEntity.getQuestionAsknum();
        asknowQuestionSetEntity.setQuestionAsknum(asknum + 1);
        //update(asknowQuestionSetEntity);
        return ResultCode.SUCCESS;
    }

    /**
     * 更新问题答案数
     *
     * @param questionId
     * @return
     */
    @Override
    public int updateQuestionAnswernum(int questionId) {
        AsknowQuestionSetEntity asknowQuestionSetEntity = get(questionId);
        if(asknowQuestionSetEntity == null) {
            return ResultCode.QUESTIONID_ERROR;
        }
        int answernum = asknowQuestionSetEntity.getQuestionAnswernum();
        asknowQuestionSetEntity.setQuestionAnswernum(answernum + 1);
        //update(asknowQuestionSetEntity);
        return ResultCode.SUCCESS;
    }


    /**
     * 获取用户问题列表
     * param pageNo 页码
     *
     * @param pageNo
     * @param pageSize 页大小
     * @param userId   用户id
     * @return
     */
    @Override
    public Pager<AsknowQuestionSetEntity> getQuestionListByUserId(int pageNo, int pageSize, int userId) {
        return findPagerByFetchedHql("from AsknowQuestionSetEntity as aqse where aqse.userId = ?", null, pageNo, pageSize, userId);
    }

    /**
     * 根据问题id获取问题分页
     * @param pageNo 页码
     * @param pageSize 页大小
     * @param questionId 问题id
     * @return
     */
    public Pager<AsknowQuestionSetEntity> getQuestionPagerByQuestionId(int pageNo, int pageSize, int questionId) {
        return findPagerByFetchedHql("from AsknowQuestionSetEntity as aqse where aqse.questionId > ?", null, pageNo, pageSize, questionId);
    }

    /**
     * 获取用户问题列表
     *
     * @param pageNo   页码
     * @param pageSize 页大小
     * @param userId   用户id
     * @return
     */
    @Override
    public Pager<AsknowQuestionSetEntity> getQuestionPagerByUserId(int pageNo, int pageSize, int userId) {
        return findPagerByFetchedHql("from AsknowQuestionSetEntity as aqse where aqse.userId = ?", null, pageNo, pageSize, userId);
    }

    /**
     * 根据问题的提问时间获取问题分页
     *
     * @param pageNo
     * @param pageSize
     * @param properties (latitude,longitude,blockCode...)
     * @return
     */
    @Override
    public Pager<AsknowQuestionSetEntity> getQuestionPagerByGeohash(int pageNo, int pageSize, Map<String, Object> properties) {
        String geohashStr = new Geohash().encode((Double)properties.get("latitude"),(Double)properties.get("longitude")).substring(0, Constants.GEO_STR_LENGTH);
        properties.put("geohash",geohashStr+ "%");
        Pager<AsknowQuestionSetEntity> pagers =  findPagerByFetchedHql("select aqse from AsknowQuestionSetEntity as aqse where aqse.asknowQuestionLocationSetEntity.geohash like :geohash  order by aqse.questionId desc", null, pageNo, pageSize, properties);
        return (pagers == null || pagers.getPageRecords().isEmpty())?findPagerByFetchedHql("from AsknowQuestionSetEntity as aqse where aqse.asknowQuestionLocationSetEntity.blockCode = :blockCode order by aqse.questionId desc",
                null,pageNo,pageSize,properties):pagers;
    }

    /**
     * 根据问题的提问时间获取问题分页,下拉刷新
     *
     * @param pageNo
     * @param pageSize
     * @param questionId
     * @param properties
     * @return
     */
    @Override
    public Pager<AsknowQuestionSetEntity> getQuestionPagerByGeohashRefresh(int pageNo, int pageSize, int questionId, Map<String, Object> properties) {
        String geohashStr = new Geohash().encode((Double)properties.get("latitude"),(Double)properties.get("longitude")).substring(0, Constants.GEO_STR_LENGTH);
        properties.put("geohash",geohashStr+ "%");
        properties.put("questionId",questionId);
        Pager<AsknowQuestionSetEntity> pagers =  findPagerByFetchedHql("select aqse from AsknowQuestionSetEntity as aqse where aqse.asknowQuestionLocationSetEntity.geohash like :geohash and aqse.questionId>:questionId order by aqse.questionId asc", null, pageNo, pageSize, properties);
        return (pagers == null || pagers.getPageRecords().isEmpty())?findPagerByFetchedHql("from AsknowQuestionSetEntity as aqse where aqse.questionId>:questionId and aqse.asknowQuestionLocationSetEntity.blockCode = :blockCode order by aqse.questionId asc",
                null,pageNo,pageSize,properties):pagers;
    }


    /**
     * 根据问题的提问时间获取问题分页
     *
     * @param pageNo
     * @param pageSize
     * @param properties (latitude,longitude,blockCode...)
     * @return
     */
    @Override
    public Pager<AsknowQuestionSetEntity> getQuestionListOrderByAskTime(int pageNo, int pageSize, Map<String, Object> properties) {
        String geohashStr = new Geohash().encode((Double)properties.get("latitude"),(Double)properties.get("longitude")).substring(0, Constants.GEO_STR_LENGTH);
        properties.put("geohash",geohashStr+ "%");
        Pager<AsknowQuestionSetEntity> pagers =  findPagerByFetchedHql("select aqse from AsknowQuestionSetEntity as aqse where aqse.asknowQuestionLocationSetEntity.geohash like :geohash order by aqse.questionAsktime desc", null, pageNo, pageSize, properties);
        return (pagers == null || pagers.getPageRecords().isEmpty())?findPagerByFetchedHql("from AsknowQuestionSetEntity as aqse where aqse.asknowQuestionLocationSetEntity.blockCode = :blockCode order by aqse.questionAsktime desc",
                null,pageNo,pageSize,properties):pagers;
    }

    /**
     * 根据问题的提问时间获取问题分页,刷新
     *
     * @param pageNo
     * @param pageSize
     * @param askTime
     * @param properties
     * @return
     */
    @Override
    public Pager<AsknowQuestionSetEntity> getQuestionListOrderByAskTimeRefresh(int pageNo, int pageSize, Timestamp askTime, Map<String, Object> properties) {
        String geohashStr = new Geohash().encode((Double)properties.get("latitude"),(Double)properties.get("longitude")).substring(0, Constants.GEO_STR_LENGTH);
        properties.put("geohash",geohashStr+ "%");
        properties.put("askTime",askTime);
        Pager<AsknowQuestionSetEntity> pagers =  findPagerByFetchedHql("select aqse from AsknowQuestionSetEntity as aqse where aqse.questionAsktime>:askTime and aqse.asknowQuestionLocationSetEntity.geohash like :geohash order by aqse.questionAsktime asc", null, pageNo, pageSize, properties);
        return (pagers == null || pagers.getPageRecords().isEmpty())?findPagerByFetchedHql("from AsknowQuestionSetEntity as aqse where aqse.questionAsktime>:askTime and aqse.asknowQuestionLocationSetEntity.blockCode = :blockCode order by aqse.questionAsktime asc",
                null,pageNo,pageSize,properties):pagers;
    }

    /**
     * 根据问题的积分数获取问题分页
     *
     * @param pageNo
     * @param pageSize
     * @param properties
     * @return
     */
    @Override
    public Pager<AsknowQuestionSetEntity> getQuestionListOrderByQuestionPoint(int pageNo, int pageSize, Map<String, Object> properties) {
//        Map<String, Object> props = getLocationInfo(pageNo,pageSize,properties);
//        //根据经纬度获取问题列表
//        List questionList =  getListBySQL("select a.questionId,a.questionAsktime,a.questionAsknum,a.questionAnswernum,a.questionContent,a.userId,a.answerId,a.questionPoint,a.questionIsSolved," +
//                "a.questionIsAlived,a.questionImageUrls,a.questionVoiceUrls,a.questionDescription,q.longitude,q.latitude,q.altitude,q.locationDetail from asknow_question_set a," +
//                "asknow_question_location_set q where a.questionId=q.questionId and (q.latitude between :minLatitude and :maxLatitude) and " +
//                "(q.longitude between :minLongitude and :maxLongitude) order by a.questionPoint desc limit :currentPage,:pageSize",props);
//
//        //若为空，则根据区号获取问题列表
//        if(questionList==null || questionList.isEmpty()) {
//            questionList = getListBySQL("select a.questionId,a.questionAsktime,a.questionAsknum,a.questionAnswernum,a.questionContent,a.userId,a.answerId,a.questionPoint,a.questionIsSolved," +
//                    "a.questionIsAlived,a.questionImageUrls,a.questionVoiceUrls,a.questionDescription,q.longitude,q.latitude,q.altitude,q.locationDetail,q.provinceCode," +
//                    "q.cityCode,q.blockCode from asknow_question_set a,asknow_question_location_set q where a.questionId=q.questionId and q.blockCode=:blockCode order by a.questionPoint desc limit :currentPage,:pageSize",props);
//        }
        String geohashStr = new Geohash().encode((Double)properties.get("latitude"),(Double)properties.get("longitude")).substring(0, Constants.GEO_STR_LENGTH);
        properties.put("geohash",geohashStr+ "%");
        Pager<AsknowQuestionSetEntity> pagers =  findPagerByFetchedHql("select aqse from AsknowQuestionSetEntity as aqse where aqse.asknowQuestionLocationSetEntity.geohash like :geohash order by aqse.questionPoint desc, aqse.questionAsktime desc", null, pageNo, pageSize, properties);
        return (pagers == null || pagers.getPageRecords().isEmpty())?findPagerByFetchedHql("from AsknowQuestionSetEntity as aqse where aqse.asknowQuestionLocationSetEntity.blockCode = :blockCode order by aqse.questionPoint desc, aqse.questionAsktime desc",
                null,pageNo,pageSize,properties):pagers;
    }


    /**
     * 根据问题的积分数获取问题分页,下拉刷新
     * @param pageNo
     * @param pageSize
     * @param questionPoint
     * @param askTime
     * @param properties
     * @return
     */
    @Override
    public Pager<AsknowQuestionSetEntity> getQuestionListOrderByQuestionPointRefresh(int pageNo, int pageSize, int questionPoint, Timestamp askTime, Map<String, Object> properties) {
        String geohashStr = new Geohash().encode((Double)properties.get("latitude"),(Double)properties.get("longitude")).substring(0, Constants.GEO_STR_LENGTH);
        properties.put("geohash",geohashStr+ "%");
        properties.put("askTime",askTime);
        properties.put("questionPoint",questionPoint);
        Pager<AsknowQuestionSetEntity> pagers =  findPagerByFetchedHql("select aqse from AsknowQuestionSetEntity as aqse where aqse.questionPoint>:questionPoint and aqse.questionAsktime>:askTime and aqse.asknowQuestionLocationSetEntity.geohash like :geohash order by aqse.questionPoint asc, aqse.questionAsktime asc", null, pageNo, pageSize, properties);
        return (pagers == null || pagers.getPageRecords().isEmpty())?findPagerByFetchedHql("from AsknowQuestionSetEntity as aqse where aqse.questionPoint>:questionPoint and aqse.questionAsktime>:askTime aqse.asknowQuestionLocationSetEntity.blockCode = :blockCode order by aqse.questionPoint asc, aqse.questionAsktime asc",
                null,pageNo,pageSize,properties):pagers;
    }

    /**
     * 根据问题答案数获取问题分页
     *
     * @param pageNo
     * @param pageSize
     * @param properties (latitude,longitude,blockCode...)
     * @return
     */
    @Override
    public Pager<AsknowQuestionSetEntity> getQuestionPagerOrderByAnswernum(int pageNo, int pageSize, Map<String, Object> properties) {
        String geohashStr = new Geohash().encode((Double)properties.get("latitude"),(Double)properties.get("longitude")).substring(0, Constants.GEO_STR_LENGTH);
        properties.put("geohash",geohashStr+ "%");
        Pager<AsknowQuestionSetEntity> pagers =  findPagerByFetchedHql("select aqse from AsknowQuestionSetEntity as aqse where aqse.asknowQuestionLocationSetEntity.geohash like :geohash order by aqse.questionAnswernum desc, aqse.questionAsktime desc", null, pageNo, pageSize, properties);
        return (pagers == null || pagers.getPageRecords().isEmpty())?findPagerByFetchedHql("from AsknowQuestionSetEntity as aqse where aqse.asknowQuestionLocationSetEntity.blockCode = :blockCode order by aqse.questionAnswernum desc, aqse.questionAsktime desc",
                null,pageNo,pageSize,properties):pagers;
    }


    /**
     * 根据问题答案数获取问题分页,下拉刷新
     * @param pageNo
     * @param pageSize
     * @param answernum
     * @param askTime
     * @param properties
     * @return
     */
    @Override
    public Pager<AsknowQuestionSetEntity> getQuestionPagerOrderByAnswernumRefresh(int pageNo, int pageSize, int answernum, Timestamp askTime, Map<String, Object> properties) {
        String geohashStr = new Geohash().encode((Double)properties.get("latitude"),(Double)properties.get("longitude")).substring(0, Constants.GEO_STR_LENGTH);
        properties.put("geohash",geohashStr+ "%");
        properties.put("answernum",answernum);
        properties.put("askTime",askTime);
        Pager<AsknowQuestionSetEntity> pagers =  findPagerByFetchedHql("select aqse from AsknowQuestionSetEntity as aqse where aqse.questionAnswernum>:answernum and aqse.questionAsktime>:askTime and aqse.asknowQuestionLocationSetEntity.geohash like :geohash order by aqse.questionAnswernum asc, aqse.questionAsktime asc", null, pageNo, pageSize, properties);
        return (pagers == null || pagers.getPageRecords().isEmpty())?findPagerByFetchedHql("from AsknowQuestionSetEntity as aqse where aqse.questionAnswernum>:answernum and aqse.questionAsktime>:askTime and aqse.asknowQuestionLocationSetEntity.blockCode = :blockCode order by aqse.questionAnswernum asc, aqse.questionAsktime asc",
                null,pageNo,pageSize,properties):pagers;
    }

    /**
     * 根据问题同问数获取问题分页
     *
     * @param pageNo
     * @param pageSize
     * @param properties (latitude,longitude,blockCode...)
     * @return
     */
    @Override
    public Pager<AsknowQuestionSetEntity> getQuestionPagerByAsknum(int pageNo, int pageSize, Map<String, Object> properties) {
        String geohashStr = new Geohash().encode((Double)properties.get("latitude"),(Double)properties.get("longitude")).substring(0, Constants.GEO_STR_LENGTH);
        properties.put("geohash",geohashStr+ "%");
        Pager<AsknowQuestionSetEntity> pagers =  findPagerByFetchedHql("select aqse from AsknowQuestionSetEntity as aqse where aqse.asknowQuestionLocationSetEntity.geohash like :geohash order by aqse.questionAsknum desc, aqse.questionAsktime desc", null, pageNo, pageSize, properties);
        return (pagers == null || pagers.getPageRecords().isEmpty())?findPagerByFetchedHql("from AsknowQuestionSetEntity as aqse where aqse.asknowQuestionLocationSetEntity.blockCode = :blockCode order by aqse.questionAsknum desc, aqse.questionAsktime desc",
                null,pageNo,pageSize,properties):pagers;
    }


    /**
     * 根据问题同问数获取问题分页,下拉刷新
     * @param pageNo
     * @param pageSize
     * @param asknum
     * @param askTime
     * @param properties
     * @return
     */
    @Override
    public Pager<AsknowQuestionSetEntity> getQuestionPagerByAsknumRefresh(int pageNo, int pageSize, int asknum, Timestamp askTime, Map<String, Object> properties) {
        String geohashStr = new Geohash().encode((Double)properties.get("latitude"),(Double)properties.get("longitude")).substring(0, Constants.GEO_STR_LENGTH);
        properties.put("geohash",geohashStr+ "%");
        properties.put("asknum",asknum);
        properties.put("askTime",askTime);
        Pager<AsknowQuestionSetEntity> pagers =  findPagerByFetchedHql("select aqse from AsknowQuestionSetEntity as aqse where aqse.questionAsknum>:asknum and aqse.questionAsktime>:askTime and aqse.asknowQuestionLocationSetEntity.geohash like :geohash order by aqse.questionAsknum asc, aqse.questionAsktime asc", null, pageNo, pageSize, properties);
        return (pagers == null || pagers.getPageRecords().isEmpty())?findPagerByFetchedHql("from AsknowQuestionSetEntity as aqse where aqse.questionAsknum>:asknum and aqse.questionAsktime>:askTime and aqse.asknowQuestionLocationSetEntity.blockCode = :blockCode order by aqse.questionAsknum asc, aqse.questionAsktime asc",
                null,pageNo,pageSize,properties):pagers;
    }

    private Map<String,Object> getLocationInfo(int pageNo, int pageSize, Map<String, Object> properties) {
        double minLatitude = (Double)properties.get("latitude")- Constants.LOCATE_DEVIATION;
        double maxLatitude = (Double)properties.get("latitude")+Constants.LOCATE_DEVIATION;
        double minLongitude = (Double)properties.get("longitude")- Constants.LOCATE_DEVIATION;
        double maxLongitude = (Double)properties.get("longitude")+Constants.LOCATE_DEVIATION;
        int currentPage = pageNo>1?(pageNo-1)*pageSize:0;
        Map<String, Object> props = new HashMap<String, Object>();
        props.put("minLatitude",minLatitude);
        props.put("maxLatitude",maxLatitude);
        props.put("minLongitude",minLongitude);
        props.put("maxLongitude",maxLongitude);
        props.put("blockCode",properties.get("blockCode"));
        props.put("currentPage",currentPage);
        props.put("pageSize",pageSize);
        return props;
    }

}
